<?php
$oJobs = JobHandler::getJobs();
$oJob = $oJobs[$_SESSION['iJobID']];
$sMissiongroups = $oJob->getMissiongroups();
$oMissions = $oJob->getMissions(true);
$sMissionStatuses = MissionHandler::getStatuses();
$oUsers = UserHandler::getUsers();

list($iFromHour, $iFromMinute) = split(':', $_REQUEST['sFromTime']);
list($iToHour, $iToMinute) = split(':', $_REQUEST['sToTime']);

$iFromTime = mktime((int)$iFromHour, (int)$iFromMinute, 0, (int)$_REQUEST['iFromMonth'], (int)$_REQUEST['iFromDay'], (int)$_REQUEST['iFromYear']);
$iToTime = mktime((int)$iToHour, (int)$iToMinute, 0, (int)$_REQUEST['iToMonth'], (int)$_REQUEST['iToDay'], (int)$_REQUEST['iToYear']);

$oJobLog = JobHandler::getLog($_SESSION['iJobID'], $iFromTime, $iToTime);

$iLastMissions = array();
foreach ($oJobLog as $i => $oEntity) {
	$iLastMissions[$oEntity['mission_id']] = $i;
}
?>
<table class="boxborder" cellspacing="0">
	<tr>
		<td class="boxhandle" colspan="3"><?php print msg('Report') ?></td>
	</tr>
	<tr>
		<td class="control small" colspan="3">
			<a href="javascript: showBox('job-makereport', 'iJobID=<?php print $_SESSION['iJobID'] ?>');"><?php print msg('New report') ?></a>
			| <a href="index.php?sView=job-view&iJobID=<?php print $_SESSION['iJobID'] ?>"><?php print msg('Back to project') ?></a>
		</td>
	</tr>
	<tr>
		<td class="listtop center"><?php print msg('Timing') ?></td>
		<td class="listtop center"><?php print msg('Action') ?></td>
		<td class="listtop"><?php print msg('Task') ?></td>
	</tr>
<?php
$oPrevEntities = array();
$bShownMissiongroups = array();
foreach ($sMissiongroups as $iMissiongroup => $sThisMissiongroup) {
	if (!in_array($iMissiongroup, $_REQUEST['bMissiongroups'])) {
		continue;
	}
	foreach ($oJobLog as $i => $oEntity) {
		if ($oEntity['group_id'] != $iMissiongroup) {
			continue;
		}
		
		// Showing the taskgroup if it hasn't been shown already.
		if (!$bShownMissiongroups[$iMissiongroup]) {
			$bShownMissiongroups[$iMissiongroup] = true;
			?>
			<tr>
				<td class="separator" colspan="3"><?php print $sThisMissiongroup ?></td>
			</tr>
			<?php
		}
		
		// Checking what type of change and whether we need to fetch some more data from the database.
		$bMissingPrevState = false;
		if (is_array($oPrevEntities[$oEntity['mission_id']])) {
			$sState = 'change';
		}
		else if ($oEntity['actiontype'] == 'create') {
			$sState = 'create';
		}
		else {
			$bMissingPrevState = true;
			$sState = 'change';
		}
		?>
		<tr>
			<td class="listentity small center"><?php print coolDate($oEntity['timing']) ?></td>
			<td class="listentity small center">
		<?php
		
		switch ($sState) {
			case 'create':
				print msg('Created');
				break;
			case 'change':
				print msg('Changed');
				break;
			default:
				print '<span class="error">'.msg('Unknown').'</span>';
		}
		?>
			</td>
			<td class="missionstatus<?php print $oEntity['status'] ?>">
		<?php
		print '<a href="index.php?sView=job-view&iJobID='.$_SESSION['iJobID'].'&iPopupMission='.$oEntity['mission_id'].'&fJobVar=showhiddenmissions&fJobValue='.$_SESSION['iJobID'].'">';
		print htmlspecialchars($oEntity['todo']);
		print '</a>';
		?>
			</td>
		</tr>
		<?php
		if ($sState == 'create') {
			?>
			<tr>
				<td class="subentity small right" colspan="2">
					<img src="images/custom/dots-disabled.png">
				</td>
				<td class="subentity small" style="padding-top: 2px; padding-bottom: 2px;">
					<?php print msg('Status') ?>:
					<span class="missionstatus<?php print $oEntity['status'] ?>"><?php print $sMissionStatuses[$oEntity['status']]['caption'] ?></span>
				</td>
			</tr>
			<tr>
				<td class="subentity small right" colspan="2">
					<img src="images/<?php print $_SESSION['oUserVars']['images'] ?>/urgency.png">
				</td>
				<td class="subentity small">
					<?php print msg('Urgency') ?>:
					<?php print $oJob->sUrgencies[$oEntity['status']] ?>
				</td>
			</tr>
			<tr>
				<td class="subentity small right" colspan="2">
					<img src="images/<?php print $_SESSION['oUserVars']['images'] ?>/missiongroup.png">
				</td>
				<td class="subentity small">
					<?php print msg('Taskgroup').': '.$sMissiongroups[$oEntity['group_id']] ?>
				</td>
			</tr>
			<tr>
				<td class="subentity small right" colspan="2">
					<img src="images/<?php print $_SESSION['oUserVars']['images'] ?>/user.png">
				</td>
				<td class="subentity small">
			<?php
			print msg('Users').': ';
			$iUsers = split('\|', trim($oEntity['users']));
			if (!$iUsers[0]) {
				print '['.msg('None').']';
			}
			else {
				$bFirst = true;
				foreach ($iUsers as $iThisUser) {
					if (!$bFirst) {
						print ', ';
					}
					print $oUsers[$iThisUser]->sUser;
					$bFirst = false;
				}
			}
			?>
				</td>
			</tr>
			<?php
		}
		else if ($sState == 'change') {
			if ($bMissingPrevState) {
				$oPrev = MissionHandler::getStateAtTime($oEntity['mission_id'], $oEntity['timing']);
			}
			else {
				$oPrev = $oPrevEntities[$oEntity['mission_id']];
			}
			
			if ($oPrev['todo'] != $oEntity['todo']) {
				?>
				<tr>
					<td class="subentity small right" colspan="2">
						<img src="images/<?php print $_SESSION['oUserVars']['images'] ?>/mission.png">
					</td>
					<td class="subentity small" style="padding-top: 2px; padding-bottom: 2px;">
						<table cellspacing="0">
							<tr>
								<td class="subentity small" style="border-style: none; padding: 0px 0px 0px 0px;">Task:&nbsp;</td>
								<td class="subentity" style="border-style: none; padding: 0px 0px 0px 0px;">
									<table class="boxborder" cellspacing="0">
										<tr>
											<td class="missionstatus0 small right"><?php print msg('Before') ?>:</td>
											<td class="missionstatus0 small"><?php print htmlspecialchars($oPrev['todo']) ?></td>
										</tr>
										<tr>
											<td class="missionstatus0 small right"><?php print msg('After') ?>:</td>
											<td class="missionstatus0 small"><?php print htmlspecialchars($oEntity['todo']) ?></td>
										</tr>
									</table>
								</td>
							</tr>
						</table>
					</td>
				</tr>
				<?php
			}
			if ($oPrev['group_id'] != $oEntity['group_id']) {
				?>
				<tr>
					<td class="subentity small right" colspan="2">
						<img src="images/<?php print $_SESSION['oUserVars']['images'] ?>/missiongroup.png">
					</td>
					<td class="subentity small">
				<?php
				$sPrevGroup = ($oPrev['group_id'] == 0 ? '[None]' : $sMissiongroups[$oPrev['group_id']]);
				$sCurrentGroup = ($oEntity['group_id'] == 0 ? '[None]' : $sMissiongroups[$oEntity['group_id']]);
				print msg('Taskgroup').': '.$sPrevGroup.' -&gt; '.$sCurrentGroup;
				?>
					</td>
				</tr>
				<?php
			}
			if ($oPrev['status'] != $oEntity['status']) {
				?>
				<tr>
					<td class="subentity small right" colspan="2">
						<img src="images/custom/dots-disabled.png">
					</td>
					<td class="subentity small" style="padding-top: 2px; padding-bottom: 2px;">
						Status:
						<span class="missionstatus<?php print $oPrev['status'] ?>"><?php print $sMissionStatuses[$oPrev['status']]['caption'] ?></span>
						-&gt;
						<span class="missionstatus<?php print $oEntity['status'] ?>"><?php print $sMissionStatuses[$oEntity['status']]['caption'] ?></span>
					</td>
				</tr>
				<?php
			}
			if ($oPrev['urgency_id'] != $oEntity['urgency_id']) {
				?>
				<tr>
					<td class="subentity small right" colspan="2">
						<img src="images/<?php print $_SESSION['oUserVars']['images'] ?>/urgency.png">
					</td>
					<td class="subentity small" style="padding-top: 2px; padding-bottom: 2px;">
						Urgency:
						<?php print $oJob->sUrgencies[$oPrev['urgency_id']] ?> -&gt; <?php print $oJob->sUrgencies[$oEntity['urgency_id']] ?>
					</td>
				</tr>
				<?php
			}
			if ($oPrev['users'] != $oEntity['users']) {
				?>
				<tr>
					<td class="subentity small right" colspan="2">
						<img src="images/<?php print $_SESSION['oUserVars']['images'] ?>/user.png">
					</td>
					<td class="subentity small">
				<?php
				print msg('Users').': ';
				$iUsers = split('\|', trim($oPrev['users']));
				if (!$iUsers[0]) {
					print '['.msg('None').']';
				}
				else {
					$bFirst = true;
					foreach ($iUsers as $iThisUser) {
						if (!$bFirst) {
							print ', ';
						}
						print $oUsers[$iThisUser]->sUser;
						$bFirst = false;
					}
				}

				print ' -&gt; ';
				
				$iUsers = split('\|', trim($oEntity['users']));
				if (!$iUsers[0]) {
					print '['.msg('None').']';
				}
				else {
					$bFirst = true;
					foreach ($iUsers as $iThisUser) {
						if (!$bFirst) {
							print ', ';
						}
						print $oUsers[$iThisUser]->sUser;
						$bFirst = false;
					}
				}
				?>
					</td>
				</tr>
				<?php
			}

		}
		
		$oPrevEntities[$oEntity['mission_id']] = $oEntity;
	}
}
?>
</table>
